package com.fesine.learn.design.pattern.creational.singleton;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @description: 类描述
 * @author: fesine
 * @createTime:2019/2/20
 * @update:修改内容
 * @author: fesine
 * @updateTime:2019/2/20
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException,
            NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //LazySingleton lazySingleton = LazySingleton.getInstance();

        Thread t1 = new Thread(new T());
        Thread t2 = new Thread(new T());
        t1.start();
        t2.start();
        System.out.println("program end");

        //序列化单例，再获取单例，测试是否是同一对象
        //HungrySingleton instance = HungrySingleton.getInstance();
        //ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
        //oos.writeObject(instance);
        //File file = new File("singleton_file");
        //ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        //HungrySingleton newInstance = (HungrySingleton)ois.readObject();
        //System.out.println(instance);
        //System.out.println(newInstance);
        //System.out.println(instance==newInstance);
        ////通过反射破坏单例
        //Class objectClass = HungrySingleton.class;
        //Constructor constructor = objectClass.getDeclaredConstructor();
        ////否则会报私有方法无法访问
        ////通过反射获取的单例不是同一个
        //constructor.setAccessible(true);
        //HungrySingleton instance = HungrySingleton.getInstance();
        //HungrySingleton newInstance = (HungrySingleton) constructor.newInstance();
        //Class objectClass = StaticInnerClassSingleton.class;
        //Constructor constructor = objectClass.getDeclaredConstructor();
        //constructor.setAccessible(true);
        //StaticInnerClassSingleton newInstance = (StaticInnerClassSingleton) constructor.newInstance();
        //StaticInnerClassSingleton instance = StaticInnerClassSingleton.getInstance();
        //System.out.println(instance);
        //System.out.println(newInstance);
        //System.out.println(instance==newInstance);
        //测试懒汉模式
        //Class objectClass = LazySingleton.class;
        //Constructor constructor = objectClass.getDeclaredConstructor();
        //constructor.setAccessible(true);
        //LazySingleton newInstance = (LazySingleton) constructor.newInstance();
        //LazySingleton instance = LazySingleton.getInstance();
        //System.out.println(instance);
        //System.out.println(newInstance);
        //System.out.println(instance==newInstance);
        //枚举类型序列化 正常
        //EnumInstance instance = EnumInstance.getInstance();
        //ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton_file"));
        //oos.writeObject(instance);
        //File file = new File("singleton_file");
        //ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        //EnumInstance newInstance = (EnumInstance)ois.readObject();
        //System.out.println(instance);
        //System.out.println(newInstance);
        //System.out.println(instance==newInstance);
        //枚举类型反射
        //Class objectClass = EnumInstance.class;
        //Constructor constructor = objectClass.getDeclaredConstructor(String.class, int.class);
        //constructor.setAccessible(true);
        //EnumInstance newInstance = (EnumInstance) constructor.newInstance("fesine",1);

        //EnumInstance instance = EnumInstance.getInstance();
        //System.out.println(instance);
        //System.out.println(newInstance);
        //System.out.println(instance==newInstance);

        //instance.printTest();
    }
}
